#####################################################################
# Product:      KnowledgeEngine (Plone Edition)
#
# File:         AddAssessmentToRepository.py
#
# Copyright:    Copyright (c) 2004, Faulkner Technologies
#
# Author:       Brian Oliver
#
# Description:  This file defines the AddAssessmentToRepository class,
#               to add an assessment to one or more repositories.
#
#               NOTE: Does not remove the assessment for it's current
#               repository
#
# See Also:     Action, Event
#####################################################################

#####################################################################
## Knowledge Engine Library Imports
from Products.KnowledgeEngine.Common import *
from Products.KnowledgeEngine.Core import *
from Products.KnowledgeEngine.Action import Action, ActionInfo
from Products.KnowledgeEngine.PropertyDefinition import *
from Products.KnowledgeEngine.PropertyTypes import *
from Products.KnowledgeEngine.PropertyFormats import *
from Products.KnowledgeEngine.PropertyStructures import *

#####################################################################
## Zope Library Imports
from Globals import DTMLFile
from App.ImageFile import ImageFile

#####################################################################
## Python Library Imports
# (none)

#####################################################################
## Private Functions (global)

#####################################################################
## AddAssessmentToRepositoryInfo Class Definition
class PythonActionInfo(ActionInfo):
    """
        Defines the class info methods, defining information about
        the class without needing an instance of the class
    """
    
    #---------------------------------------------------------------#
    
    def getClassName(self):
        """
            Returns the name of the class for which 
            we are providing information.
        """
        return "PythonAction"

    #---------------------------------------------------------------#

    def getDisplayName(self):
        """
            Return a friendly name for the class instances
            (this is used for displaying in lists etc)
        """
    
        return "Python Action"
        
    #---------------------------------------------------------------#

    def getIdentityType(self):
        """
            Return the identity type.  
        """
        
        return "pyaction"
        
    #---------------------------------------------------------------#
    
    def getClassIcon(self):
        """
            Return the appropriate icon file for the class.
        """
        
        return ImageFile('skins/PythonAction_icon.gif', globals())
   
    #---------------------------------------------------------------#

    def onConfigureProperties(self):
        """
            Automatically called by the Knowledge Engine once
            the class has been loaded.  Use this method to register 
            class specific properties using the PropertyReflection interface.
            
            If this class is sub-classed, a call must be made to
            the super-class to ensure super-class properties are
            correctly registered
            
            NOTE: This method is called before ClassInfo.onAfterStartup()            
        """

        #initialise the super-class properties for reflection
        ActionInfo.onConfigureProperties(self)
        
        self.addPropertyDefinition(PropertyDefinition(
            identity="script", 
            displayName="Script", 
            description="The python script to execute", 
            defaultValue="", 
            propertyType=pt_STRING, propertyFormat=pf_TEXT_MULTILINE, propertyStructure=ps_ATOMIC
        ))
                
        

    #---------------------------------------------------------------#

#####################################################################
## AddAssessmentToRepository Action Class Definition
class PythonAction(Action):
    """
        A class to represent a simple folder that may contain Elements
    """

    #---------------------------------------------------------------#

    #ABSTRACT TRANSIENT: a ClassInfo implementation instance to provide info about this class
    _CLASSINFO = PythonActionInfo()
    
    #---------------------------------------------------------------#

    def __init__(self, id, identity):
        "Constructor for the Folder"        

        Action.__init__(self, id, identity)
                
        #perform onAfterInstanciation processing
        self.onAfterInstanciation()

    #---------------------------------------------------------------#

    def onAfterInstanciation(self):
        """
            Handle Knowledge Engine specific actions after this object 
            has been loaded into memory.  
            
            This is useful for performing backwards compatibility
        """        
        
        #perform default processing
        Action.onAfterInstanciation(self)
    
    #---------------------------------------------------------------#
    
    def execute(self, assessment, user = None, previousActionSuccess = true):
        """
           Adds the assessment to the repositories specified by this
           action
        """            

        #let's grab the model so we can do some work
        model = assessment.getModel()
        
        #get the assessment storage service (so we can add the assessment to the specified repositories)
        assessmentStorageService = model.getAssessmentStorageService()
        
        return self.executeScript( assessment, namespace=locals() )
        

        
        
    #---------------------------------------------------------------#

    def executeScript(self, assessment, namespace=None):
        """ Executes the python script """
        
        import new
        
        script = self.script
        
        script = script.replace("\r\n","\n")
        
        context = self
        
        if not namespace: namespace = {}
        
        namespace.update({'context': self, 
                     'user': context.REQUEST.AUTHENTICATED_USER,
                     'request': context.REQUEST,
                     'assessment': assessment,
                    })
                     
        
        exec script in namespace
        
        return namespace.get('RETURN', None)

#####################################################################
## Class Initialisation

# Register Class Information with the Knowledge Engine
registerGenericObjectClass(PythonAction)

#####################################################################